import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'login',
      component: () => import('../views/Login.vue'),
    },
    {
      path: '/home',
      name: 'home',
      component: () => import('../views/Home.vue'),
      redirect: '/home/data-boat',
      children: [
        {
          path: 'data-boat',
          name: 'data-boat',
          component: () => import('../views/DataBoat.vue'),
        },
        {
          path: 'sort',
          name: 'sort-manager',
          component: () => import('../views/SortManager.vue'),
        },
        {
          path: 'user',
          name: 'user-manager',
          component: () => import('../views/UserManager.vue'),
        },
        {
          path: 'log',
          name: 'log-manager',
          component: () => import('../views/LogManager.vue'),
        },
        {
          path: 'index',
          name: 'index-manager',
          component: () => import('../views/IndexManager.vue'),
        },
        {
          path: 'spot',
          name: 'spot-manager',
          component: () => import('../views/SpotManager.vue'),
        },
        {
          path: 'service',
          name: 'service-manager',
          component: () => import('../views/ServiceManager.vue'),
        },
        {
          path: 'import',
          name: 'import-manager',
          component: () => import('../views/ImportManager.vue'),
        },
        {
          path: 'notice',
          name: 'notice-manager',
          component: () => import('../views/NoticeManager.vue'),
        },
        {
          path: 'notice-banner',
          name: 'notice-banner-manager',
          component: () => import('../views/NoticeBannerManager.vue'),
        },
        // 其他子路由可以在这里添加
      ],
    },
  ],
})

// 全局前置守卫，检查登录状态
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')

  // 如果访问的不是登录页，且没有登录凭证，则重定向到登录页
  if (to.path !== '/' && !token) {
    next('/')
  } else {
    next()
  }
})

export default router
